package com.aisile.admin.service.impl;

import com.aisile.admin.mapper.AdChannelMapper;
import com.aisile.admin.service.AdChannelService;
import com.aisile.common.exception.CustomException;
import com.aisile.common.exception.CustomExceptionCatch;
import com.aisile.model.admin.dtos.ChannelDto;
import com.aisile.model.admin.pojos.AdChannel;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Auther:yry
 * @Date:2023/12/20 0020
 * @VERSON:1.0
 */
@Service

@SuppressWarnings("ALL")
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findAllByPage(ChannelDto dto) {
        // 1. 无脑判断、先判断错  json = {}
        if(dto == null){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_NOT_EXIST_TEST);
        }
        // 分页判断 初始化分页参数
        dto.checkParam();
        // 开始拼接分页条件
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(dto.getName()),AdChannel::getName,dto.getName());
        // 分页
        Page page = new Page(dto.getPage(),dto.getSize());
        IPage<AdChannel> iPage =  this.baseMapper.selectPage(page,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),((Long)iPage.getTotal()).intValue());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult saveAdChannel(AdChannel adChannel) {
        // 无脑判断  // 双重校验
        if(adChannel == null ||adChannel.getName()==null || "".equals(adChannel.getName().trim())){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_NOT_EXIST_TEST);
        }
        // 幂等性（保证数据唯一、采用先查后插方式）
        int i = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,adChannel.getName()));
        if(i>0){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_EXIST);
        }
        // 可以插入对象了
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @Transactional  // 传播行为（7个）   隔离级别（5个 读以提交）
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        // 什么时候加 事务  什么时候不加事务？  是否是一个整体业务才考虑使用事务
        // 当方法中 有多个数据库操作的时候 才考虑使用Transactional 前提，二个DML语句方式才行
        // 增删改的时候加事务

        if(adChannel == null||adChannel.getName()==null || "".equals(adChannel.getName().trim()) || adChannel.getId()==null){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_NOT_EXIST_TEST);
        }
        AdChannel adChannelOld =  this.getById(adChannel.getId());
        if(adChannelOld == null){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_NOT_EXIST_TEST);
        }
        // 直接修改
        this.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateStatusByType(Integer id, Integer type) {
        if(id == null || type == null){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_NOT_EXIST_TEST);
        }
        if(type>1 || type<0){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel =  this.getById(id);
        if(adChannel == null){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.PARAM_INVALID);
        }
        // 直接修改 三目运算符
        adChannel.setStatus(type==0?false:true);
        this.updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteAdChannelById(Integer id) {
        if(id == null ){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.DATA_NOT_EXIST_TEST);
        }
        AdChannel adChannel =  this.getById(id);
        if(adChannel == null){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.PARAM_INVALID);
        }
        // true 有效  有效不能删除
        if(adChannel.getStatus()){
            CustomExceptionCatch.catchAdmin(AdminHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
